# app/api/trading/models.py
from gate_api import FuturesOrder, Configuration, ApiClient, FuturesApi
from gate_api.exceptions import ApiException, GateApiException


def create_order(gate_key, gate_secret, settle, contract, size, price, tif=None, close=None, text='',
                 reduce_only=False, auto_size=None):
    configuration = Configuration(
        host="https://api.gateio.ws/api/v4",
        key=gate_key,
        secret=gate_secret
    )

    api_client = ApiClient(configuration)
    api_instance = FuturesApi(api_client)

    # 构建订单对象，确保所有参数都正确设置
    order = FuturesOrder(
        contract=contract,
        size=size,
        price=str(price),  # 确保价格被正确地转换为字符串
        tif=tif,
        close=close,
        text=text,
        reduce_only=reduce_only,
        auto_size=auto_size  # 添加对双仓操作的支持
    )

    try:
        # 创建订单
        response = api_instance.create_futures_order(settle, order)
        return {"code": 200, "data": response.to_dict(), "msg": "订单创建成功"}
    except GateApiException as ex:
        return {"code": ex.status, "msg": f"Gate API异常, 标签: {ex.label}, 信息: {ex.message}"}
    except ApiException as e:
        return {"code": 500, "msg": f"API调用异常: {e}"}


def query_personal_trading_history(gate_key, gate_secret, settle, contract=None, order=None, limit=100, offset=0,
                                   last_id=None):
    configuration = Configuration(
        host="https://api.gateio.ws/api/v4",
        key=gate_key,
        secret=gate_secret
    )

    api_client = ApiClient(configuration)
    api_instance = FuturesApi(api_client)

    try:
        api_response = api_instance.get_my_trades(
            settle, contract=contract, order=order, limit=limit, offset=offset, last_id=last_id
        )
        return {"code": 200, "data": [trade.to_dict() for trade in api_response], "msg": "交易历史查询成功"}
    except GateApiException as ex:
        return {"code": ex.status, "msg": f"Gate API异常, 标签: {ex.label}, 信息: {ex.message}"}
    except ApiException as e:
        return {"code": 500, "msg": f"API调用异常: {e}"}


def query_orders(gate_key, gate_secret, settle, status, contract=None, limit=100, offset=0, last_id=None):
    configuration = Configuration(
        host="https://api.gateio.ws/api/v4",
        key=gate_key,
        secret=gate_secret
    )
    api_client = ApiClient(configuration)
    api_instance = FuturesApi(api_client)

    try:
        api_response = api_instance.list_futures_orders(settle, status, contract=contract, limit=limit, offset=offset,
                                                        last_id=last_id)
        return {"code": 200, "data": [order.to_dict() for order in api_response], "msg": "订单查询成功"}
    except GateApiException as ex:
        return {"code": ex.status, "msg": f"Gate API异常, 标签: {ex.label}, 信息: {ex.message}"}
    except ApiException as e:
        return {"code": 500, "msg": f"API调用异常: {e}"}

def cancel_order(gate_key, gate_secret, settle, order_id):
    configuration = Configuration(
        host="https://api.gateio.ws/api/v4",
        key=gate_key,
        secret=gate_secret
    )
    api_client = ApiClient(configuration)
    api_instance = FuturesApi(api_client)

    try:
        # 取消指定的订单
        api_response = api_instance.cancel_futures_order(settle, order_id)
        return {"code": 200, "data": api_response.to_dict(), "msg": "订单取消成功"}
    except GateApiException as ex:
        return {"code": ex.status, "msg": f"Gate API异常, 标签: {ex.label}, 信息: {ex.message}"}
    except ApiException as e:
        return {"code": 500, "msg": f"API调用异常: {e}"}
